package com.example.smart.ui.activity.animal

import com.example.lib.constants.JumpConstants
import com.example.lib.extend.autoInt
import com.example.lib.extend.getIntentInt
import com.example.lib.extend.getIntentSerializable
import com.example.lib.extend.gone
import com.example.lib.extend.gridManager
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.extend.stringText
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.adapter.other.ChooseImageAdapter
import com.example.smart.base.ui.activity.BaseTitleBarActivity
import com.example.smart.data.AnimalData
import com.example.smart.databinding.ActivityAddAnimalBinding
import com.example.smart.dialog.DateDialog
import com.example.smart.dialog.FySuccessDialog
import com.example.smart.dialog.SexDialog
import com.example.smart.room.bean.AnimalBean
import com.example.smart.room.factory.RoomFactory
import com.example.smart.utils.DisplayUtils
import com.example.smart.utils.handler.HandUtils

class AddAnimalActivity : BaseTitleBarActivity<ActivityAddAnimalBinding>() {
    private val mAnimalResponse = RoomFactory.createAnimalRepository()
    private var id = 0

    private val mImageAdapter by lazy {
        ChooseImageAdapter(this@AddAnimalActivity)
    }

    private val mDateDialog by lazy {
        DateDialog(this@AddAnimalActivity)
    }

    private val mSexDialog by lazy {
        SexDialog(this@AddAnimalActivity)
    }

    private val mFyDialog by lazy {
        FySuccessDialog(this@AddAnimalActivity)
    }

    override fun layoutID() = R.layout.activity_add_animal

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@AddAnimalActivity, statusBarColor = com.example.lib.R.color.white)
        binding.ImageRecycler.gridManager(3)
        binding.ImageRecycler.adapter = mImageAdapter
        id = getIntentInt(JumpConstants.ID)
        if (id == 1) {
            setTitle("确认牲畜信息")
            binding.Confirm.text = "开具防疫证明"
            binding.Ear.isEnabled = false
            binding.Birthday.isEnabled = false
            binding.Type.isEnabled = false
            binding.Sex.isEnabled = false
            binding.Weight.isEnabled = false
            binding.Source.isEnabled = false
            binding.DnDay.isEnabled = false
            binding.SexCount.isEnabled = false
            mImageAdapter.updateList(mutableListOf(), false)
        } else if (id == 2) {
            setTitle("确认牲畜信息")
            binding.Confirm.text = "填写牲畜来向"
            binding.Ear.isEnabled = false
            binding.Birthday.isEnabled = false
            binding.Type.isEnabled = false
            binding.Sex.isEnabled = false
            binding.Weight.isEnabled = false
            binding.Source.isEnabled = false
            binding.DnDay.isEnabled = false
            binding.SexCount.isEnabled = false
            mImageAdapter.updateList(mutableListOf(), false)
        } else {
            setTitle("详情")
        }
    }

    override fun initData() {
        getIntentSerializable<AnimalData>(JumpConstants.Bean)?.let { bean ->
            binding.Ear.setText(bean.earNo)
            binding.Birthday.setText(bean.birthday)
            binding.Type.setText(bean.type)
            binding.Sex.setText(bean.sex)
            binding.Weight.setText(bean.weight)
            binding.Source.setText(bean.source)
            binding.DnDay.setText(bean.dnDay)
            binding.SexCount.setText(bean.sexCount)

            binding.TagImage.setPadding(0, 0, 0, 0)
            binding.TagImage.setImageResource(R.mipmap.icon_after_scan)
            binding.TagTitle.text = String.format(getString(R.string.scan_format), bean.type, bean.earNo)
            binding.TagMessage.text = getString(R.string.jy_done)
            binding.TagTry.gone()
        }
    }

    override fun initListener() {
        mDateDialog.onResult = { type, date ->
            if (type == 0) {
                binding.Birthday.text = date
            } else {
                binding.DnDay.text = date
            }
        }

        mSexDialog.onResult = {
            binding.Sex.text = it
        }

        binding.TagTry.setOnDoubleClickListener {
            DisplayUtils.toAddAnimalChoose(this@AddAnimalActivity)
        }

        binding.Birthday.setOnDoubleClickListener {
            mDateDialog.show(0)
        }

        binding.DnDay.setOnDoubleClickListener {
            mDateDialog.show(1)
        }

        binding.Sex.setOnDoubleClickListener {
            mSexDialog.show()
        }

        binding.Confirm.setOnDoubleClickListener {
            if (id == 1) {
                mFyDialog.show()
            } else if (id == 2) {
                DisplayUtils.toYsDetails(this@AddAnimalActivity)
            } else {
                val ear = binding.Ear.stringText()
                val birthday = binding.Birthday.stringText()
                val type = binding.Type.stringText()
                val sex = binding.Sex.stringText()
                val weight = binding.Weight.stringText()
                val source = binding.Source.stringText()
                val dnDay = binding.DnDay.stringText()
                val sexCount = binding.SexCount.stringText()
                if (ear.isEmpty() || birthday.isEmpty() || type.isEmpty() || sex.isEmpty() || weight.isEmpty() || source.isEmpty() || dnDay.isEmpty() || sexCount.isEmpty()) {
                    ToastUtils.showToast(getString(R.string.input_non_null))
                    return@setOnDoubleClickListener
                }

                mAnimalResponse.insert(AnimalBean(ear.autoInt(),
                    birthday,
                    type,
                    sex,
                    weight,
                    source,
                    dnDay,
                    sexCount,
                    mImageAdapter.getImageList().firstOrNull()))
                binding.Confirm.isEnabled = false
                ToastUtils.showToast(getString(R.string.add_success))
                HandUtils.postTask({
                    finish()
                }, 1000)
            }
        }
    }
}